/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmail.file;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Workbook;
import jxl.write.*;

/**
 *
 * @author iflamed
 */
public class DumpExcel implements Runnable {

    private File file;
    private WritableWorkbook workbook;
    private WritableSheet sheet;
    private List<Vector> data;
    /**
     *
     * @param filepath the excel xls file path will be stored
     */
    public DumpExcel(String filepath) throws IOException {
        file = new File(filepath);
        workbook = Workbook.createWorkbook(file);
        sheet = workbook.createSheet("First Sheet", 0);
    }

    public void writeExcel() {
        try {
            Label label = new Label(0, 2, "A label record");
            sheet.addCell(label);

            jxl.write.Number number = new jxl.write.Number(3, 4, 3.1459);
            sheet.addCell(number);
            workbook.write();
            workbook.close();
        } catch (IOException ex) {
            Logger.getLogger(DumpExcel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (WriteException ex) {
            Logger.getLogger(DumpExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void writeExcel(List<Vector> data) {
        Iterator it = data.iterator();
        try {
            for (int i = 0; it.hasNext(); i++) {
                Vector alldata = (Vector) it.next();
                if (i == 0) {
                    Iterator itdata = alldata.iterator();
                    for (int j = 0; itdata.hasNext(); j++) {
                        Label label = new Label(j, i, (String) itdata.next());
                        sheet.addCell(label);
                    }
                } else {
                    Iterator itdata = alldata.iterator();
                    for (int j = 0; itdata.hasNext(); j++) {
                        if (j == 0 || j == 1 || j == 20) {
                            Label label = new Label(j, i, (String) itdata.next());
                            sheet.addCell(label);
                        } else {
                            WritableCellFormat integerFormat = new WritableCellFormat(NumberFormats.INTEGER);
                            int d = (Integer) itdata.next();
                            jxl.write.Number number = new jxl.write.Number(j, i, d, integerFormat);
                            sheet.addCell(number);
                        }
                    }
                }
            }
            this.workbook.write();
            this.workbook.close();
        } catch (IOException ie) {
            Logger.getLogger(DumpExcel.class.getName()).log(Level.SEVERE, null, ie);
        } catch (WriteException we) {
            Logger.getLogger(DumpExcel.class.getName()).log(Level.SEVERE, null, we);
        }
    }

    public void setData(List<Vector> adata) {
        this.data = adata;
    }

    
    @Override
    public void run() {
        this.writeExcel(data);
    }
}
